package com.yimixian.app.ui;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.yimixian.app.R;
import com.yimixian.app.common.Constants;
import com.yimixian.app.data.DataManager;
import com.yimixian.app.model.ImageInfo;
import com.yimixian.app.util.UiUtils;
import com.ymx.sdk.util.DensityUtils;
import com.ymx.sdk.util.DeviceUtils;

import java.util.List;

/**
 * Created by zhilian-2 on 2015/10/15.
 */
public class MetroWrapView extends ViewGroup {

    Picasso picasso;
    private DataManager mDataManager;
    HomeHeadView.OnHeadViewClickListener mHeadViewClickListener;
    private static final int PADDING_HOR = 0;//水平方向padding
    private static final int PADDING_VERTICAL = 5;//垂直方向padding
    private static final int SIDE_MARGIN = 0;//左右间距
    private static final int TEXT_MARGIN = 0;
    int screenWidth;
    private static final int columnCount = 4;//列数
    int linHeight = 0;
    /**
     * @param context
     */
    public MetroWrapView(Context context) {
        super(context);
        initView();
    }

    /**
     * @param context
     * @param attrs
     * @param defStyle
     */
    public MetroWrapView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView();
    }



    /**
     * @param context
     * @param attrs
     */
    public MetroWrapView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public void initView(){
        picasso = Picasso.with(getContext());
        screenWidth = DeviceUtils.getScreenWidth(getContext());
        mDataManager = DataManager.getInstance();
//        linHeight = DensityUtils.dp2px(getContext(), 110);
    }

    public void bind(List<ImageInfo> mList, float ratio, final HomeHeadView.OnHeadViewClickListener
            mHeadViewClickListener){
        if (mList == null) return;
        if (screenWidth <= 0) screenWidth = DeviceUtils.getScreenWidth(getContext());
        setBackgroundResource(R.color.white);
        this.mHeadViewClickListener = mHeadViewClickListener;
        for (int i = 0; i < mList.size(); i++) {
            ImageInfo imageInfo = mList.get(i);
            View metroView = LayoutInflater.from(getContext()).
                    inflate(R.layout.home_metro_item, this, false);
//            metroView.setBackgroundResource(R.drawable.settings_item_bg);
            int height = (int) (screenWidth / (4 * ratio));
            if (height <= 0) height = screenWidth / 4;
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    screenWidth / 4, height);
            ImageView imageView = (ImageView) metroView.findViewById(R.id.image);
            metroView.setTag(i);
            imageView.setLayoutParams(params);
            metroView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mHeadViewClickListener != null)
                        mHeadViewClickListener.OnHeadViewClick(Constants.HeadViewType.Metro, (int) view.getTag());
                }
            });
            picasso.load(mDataManager.getImageUrl(imageInfo.image, screenWidth / 4))
                    .placeholder(R.drawable.placeholder)
                    .into(imageView);
            this.addView(metroView);
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childCount = getChildCount();
        int autualWidth = r - l;
        int x = SIDE_MARGIN;// 横坐标开始
        int y = 0;//纵坐标开始
        int rows = 1;
        for(int i=0;i<childCount;i++){
            View view = getChildAt(i);
//            if (i%2 == 0) view.setBackgroundColor(Color.GREEN);
//            else view.setBackgroundColor(Color.YELLOW);
            int width = screenWidth/columnCount;
            int height = view.getMeasuredHeight();
            x += width+TEXT_MARGIN;
            if(x>autualWidth){
                x = width+SIDE_MARGIN;
                rows++;
            }
            y = rows*(height+TEXT_MARGIN);
            if(i==0){
                view.layout(x-width-TEXT_MARGIN, y-height, x-TEXT_MARGIN, y);
            }else{
                view.layout(x-width, y-height, x, y);
            }
        }
    };

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int x = 0;//横坐标
        int y = 0;//纵坐标
        int rows = 1;//总行数
        int specWidth = MeasureSpec.getSize(widthMeasureSpec);
        int actualWidth = specWidth - SIDE_MARGIN * 2;//实际宽度
        int childCount = getChildCount();
        for(int index = 0;index<childCount;index++){
            View child = getChildAt(index);
            child.setPadding(PADDING_HOR, PADDING_VERTICAL, PADDING_HOR, PADDING_VERTICAL);
            child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
            int width = screenWidth/columnCount;
            int height = child.getMeasuredHeight();
            x += width+TEXT_MARGIN;
            if(x>actualWidth){//换行
                x = width;
                rows++;
            }
            y = rows*(height+TEXT_MARGIN);
        }
        setMeasuredDimension(actualWidth, y);
    }

}